package xiegd.study.lambda;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: xieguangda
 * @Description:
 * @Date: 2023/7/27 11:22
 */
public class StudentLambdaTest {

    public static void main(String[] args) {
        List<Student> students = init();

//        Map<Integer, List<Man>> collect = students.stream().map(e -> {
//            Man man = new Man();
//            man.setName(e.getName());
//            man.setAge(e.getAge());
//            return man;
//        }).collect(Collectors.groupingBy(Man::getAge));
//        System.out.println("我是我的测试看看：：" + JSONObject.toJSONString(collect));

        Map<Integer, List<Man>> collect = students.stream()
                .collect(Collectors.groupingBy(Student::getAge,
                        Collectors.mapping(e -> {
                            Man man = new Man();
                            man.setName(e.getName());
                            return man;
                        }, Collectors.toList())));

        System.out.println("我是我的测试看看：：" + JSONObject.toJSONString(collect));


        Map<Integer, List<String>> collect2 = students.stream()
                .collect(Collectors.groupingBy(Student::getAge,
                        Collectors.mapping(e -> {
                            return e.getName();
                        }, Collectors.toList())));

        System.out.println("我是我的测试看看222：：" + JSONObject.toJSONString(collect2));
    }




    public static List<Student> init(){
        List<Student> students = new ArrayList<>();
        students.add(Student.builder().age(11).name("t1").build());
        students.add(Student.builder().age(11).name("t2").build());
        students.add(Student.builder().age(11).name("t3").build());
        students.add(Student.builder().age(12).name("t4").build());
        return students;
    }

}

@Data
@Builder
class Student{
    private Integer age;

    private String name;
}

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
class Man{
    private String name;
}
